//https://www.luogu.com.cn/problem/P1347
//RE 8,OK 2
#include<cstdio>

//(from,to)

struct graph{
	int N,M;
	char letter[27];
	char E[26][26];
	graph(int _N=26){
		N=_N;
		for(int from=0;from<N;++from){
			for(int to=0;to<N;++to){
				E[from][to]=0;
			}
		}
	}
	int in_degree(int to){
		int count=0;
		for(int from=0;from<N;++from){
			if(E[from][to]){++count;}
		}
		return count;
	}
	int out_degree(int from) {
		int count = 0;
		for(int to = 0;to < N;++to) {
			if(E[from][to]) { ++count; }
		}
		return count;
	}
	// void show(){
	// 	letter[N]=0;
	// 	printf("%d : %s\n",N,letter);
	// }
};
graph Total;

enum Result:char{
NOT_YET=1,OK=0,ERROR=-1,VALID=2
};

Result check_validity(graph& G) {
	bool have_empty_node=false;
	if(G.N <= 1) { return VALID; }
	for(int i = 0;i < G.N;++i) {
		if(G.E[i][i]) { return ERROR; }
		int ind = G.in_degree(i), oud = G.out_degree(i);
		if(ind == 0 && oud != 0) {
			return VALID;
		}
		else if(ind==0&&oud==0){
			have_empty_node=true;
		}
	}
	return have_empty_node?VALID:ERROR;
}
Result check_orderable(graph& G,char* buf){
	//G.show();
	if(!check_validity(G)){return ERROR;}
	if(G.N<=1){
		*buf=G.letter[0];
		return OK;
	}
	int count=0;int i=0,already=-1;
	for(;i<G.N;++i){
		if(G.in_degree(i)==0){
			already=i;
			
			count=1;
			break;
		}
	}
	if(count==0){return ERROR;}
	*buf=G.letter[already];
	graph new_graph(G.N-1);
	
	for(int i=0, i_to=0;i<G.N;++i){
		if(i==already){continue;}
		new_graph.letter[i_to]=G.letter[i];
		++i_to;
	}

	for(int to=0,to_f=0;to_f<G.N;++to_f){
		if(to_f==already){continue;}
		for(int from=0,from_f=0;from_f<G.N;++from_f){
			if(from_f==already){continue;}
			new_graph.E[from][to]=G.E[from_f][to_f];
			++from;
		}
		++to;
	}
	Result res=check_orderable(new_graph,buf+1);
	return res;
}

inline void init(char* buf){
	for(int i=0;i<27;++i){
		buf[i]=0;
	}
}
int main(){
	char buf[27];
	Result res;
	scanf("%d%d", &Total.N, &Total.M);
	for(int i = 0;i < Total.N;++i) {
		Total.letter[i] = i + 'A';
	}
	
	int round=0;
	for(;round < Total.M;++round) {
		char low, high;
		//getchar();//吃掉回车
		getchar();//吃掉回车
		scanf("%c<%c", &low, &high);
		Total.E[low - 'A'][high - 'A'] = 1;
		init(buf);
		if(round==31){
			int sss=0;
		}
		res=check_orderable(Total,buf);
		if(res!=NOT_YET){
			break;
		}
	}
	switch(res){
		case OK:printf("Sorted sequence determined after %d relations: %s.",round+1,buf);
		break;
		case NOT_YET:printf("Sorted sequence cannot be determined.");
		break;
		case ERROR:printf("Inconsistency found after %d relations.",round+1);
	}
	return 0;
}